/**
 * Created by dale on 2017/10/17.
 */
var myMeasue;
var nowsBoxCode;
var ableFlag;
var flag=true;
var targetObj;
$(document).ready(function () {

    $("#iTypeId").combobox({  //商品类型层
        onSelect:function (rec) {
            var id = rec['value'];
            $('#iProdId').combobox({   //商品层
                url:baseUrl+'ims/moblie/getProds?id='+id,
                valueField:'iProdId',
                textField:'sProdName',
                onSelect:function (record) {

                    $('#sMeasure').combobox({  //量词层
                        url:baseUrl+'ims/moblie/getMeasue?iProdId='+record['iProdId'],
                        valueField:'id',
                        textField:'text',
                        onSelect:function (meas) { //只,箱
                            myMeasue=meas;
                            if(flag){
                                if(meas['id']=='只'||meas['id']=='支'||meas['id']=='个'||meas['id']=='本'||meas['id']=='包'||meas['id']=='台'||meas['id']=='张'||meas['id']=='瓶'||meas['id']=='支'){
                                    append(1,meas['id'],meas['measureType']);
                                }else if(meas['id']=='箱'||meas['id']=='袋'||meas['id']=='条'||meas['id']=='盒'){
                                    append(2,meas['id'],meas['measureType']);
                                    $('#iProdNum').numberbox('setValue', meas['num']);
                                }
                            }else {
                                targetObj.remove();
                                if(meas['id']=='只'||meas['id']=='支'||meas['id']=='个'||meas['id']=='本'||meas['id']=='包'||meas['id']=='台'||meas['id']=='张'||meas['id']=='瓶'||meas['id']=='支'){
                                    append(1,meas['id'],meas['measureType']);
                                }else if(meas['id']=='箱'||meas['id']=='袋'||meas['id']=='条'||meas['id']=='盒'){
                                    append(2,meas['id'],meas['measureType']);
                                    $('#iProdNum').numberbox('setValue', meas['num']);
                                }
                            }

                        }
                    })

                }
            });
        }
    });

    function append($type,$measure,$measureType) {
        var content='';
        if($type==1){
            content = '<div style="margin-bottom:10px">'
                +'<input id="sProdCode" class="easyui-textbox" label="'+$measure+'码:"  name="sProdCode" data-options="buttonText:\'扫描\',buttonIcon:\'icon-search\',prompt:\'扫描货码\'" style="width:100%">'
                +'</div>'
        }
        if($type==2){
            content = '<div style="margin-bottom:10px">'
                +'<input id="sBoxCode" class="easyui-textbox" label="'+$measure+'码:"  name="sBoxCode" data-options="buttonText:\'扫描\',buttonIcon:\'icon-search\',prompt:\'扫描货码\'" style="width:100%">'
                +'</div>';
            content += '<div style="margin-bottom:10px">'
                +'<input id="iProdNum" class="easyui-numberbox" label="'+$measureType+':"  name="iProdNum"   style="width:100%">'
                +'</div>';
        }
        if($type==3){
            content += '<div style="margin-bottom:10px">'
                +'<input id="sProdCode" class="easyui-textbox" label="'+$measure+'码:"  name="sProdCode" data-options="buttonText:\'扫描\',buttonIcon:\'icon-search\',prompt:\'扫描货码\'" style="width:100%">'
                +'</div>';
            var boxName=getMeasure($measureType,2);
            content += '<div style="margin-bottom:10px">'
                +'<input id="sBoxCode" class="easyui-textbox" label="'+boxName+'码:"  name="sBoxCode"  style="width:100%">'
                +'</div>';

        }

        content +='<div style="margin-bottom:10px">';
        if($type==2){

            content +='<a id="DetailIn" class="easyui-linkbutton" data-options="iconCls:\'icon-detail\'" style="padding:5px 0px;width:100px;float: right" >'
                +'<span style="font-size:14px;">明细</span>'
                +'</a>';
        }
        if($type==3){
            content +='<a id="back" class="easyui-linkbutton" data-options="iconCls:\'icon-undo\'" style="padding:5px 0px;width:100px;float: right" >'
                +'<span style="font-size:14px;">返回</span>'
                +'</a>';
        }
        content +='<a id="saveIn" class="easyui-linkbutton" data-options="iconCls:\'icon-ok\'" style="padding:5px 0px;width:100px;float: right;margin-right: 5px;">'
            +'<span style="font-size:14px;">提交</span>'
            +'</a>'
            +'</div>';

        targetObj = $(content).appendTo("#ff");
        $.parser.parse(targetObj);
        //
        if($type == 2){
            $('#DetailIn').hide();
        }
        if($type==3){
            $('#sMeasure').combobox('disable');
            $('#iTypeId').combobox('disable');
            $('#iProdId').combobox('disable');
            $('#sBoxCode').textbox('setValue',nowsBoxCode).textbox('disable');
            $('#back').hide();
        }
        flag=false;
        return targetObj;
    }

    $('#ff').on('click','#DetailIn',function () {
        // document.getElementById("header").innerHTML='入库明细';
        ableFlag=true;
        if($('#sBoxCode').textbox('getValue')==''){
            $.messager.alert('错误','物品码为空','info');
            return false;
        }else {
            nowsBoxCode=$('#sBoxCode').textbox('getValue');
        }
        targetObj.remove();
        var id=getMeasure(myMeasue['measureType'],1);
        $('#sMeasure').combobox('setValue',id); //设置只
        targetObj.remove();//移除只创建
        append(3,id,myMeasue['measureType']);
        
        getBoxNum(nowsBoxCode);
    });

    $('#ff').on('click','#back',function () {
        // document.getElementById("header").innerHTML='入库';
        ableFlag=false;
        targetObj.remove();
        var id=getMeasure(myMeasue['measureType'],false);
        $('#sMeasure').combobox('setValue',id).combobox('enable');
        $('#iTypeId').combobox('enable');
        $('#iProdId').combobox('enable');
        $('#sBoxCode').textbox('setValue',nowsBoxCode).textbox('enable');
    });


    $('#code').on('change', function () {

    });





    $('#ff').on('click','#saveIn',function () {  //提交表单
        if(ableFlag){
            $('#sMeasure').combobox('enable');
            $('#iTypeId').combobox('enable');
            $('#iProdId').combobox('enable');
            $('#sBoxCode').textbox('enable');
        }
        if($('#iTypeId').combobox('getText') == '平板' && $('#sMeasure').combobox('getValue') == '箱' && $('#sBoxCode').textbox('getValue') == ''){  //只有平板需要验证
            $.messager.alert('错误','箱码为空','error');
            return false;
        }

        if($('#iTypeId').combobox('getText') == '平板' && $('#sMeasure').combobox('getValue') == '只' && $('#sProdCode').textbox('getValue') == ''){  //只有平板需要验证
            $.messager.alert('错误','只码为空','error');
            return false;
        }

        $.ajax({
            url:baseUrl+'ims/moblie/saveIn',
            data:$('#ff').serialize(),
            type:'post',
            success:function (data) {

                $.messager.alert('成功',data,'info');
                if($('#DetailIn') != null && $('#iTypeId').combobox('getText') == '平板'){
                    $('#DetailIn').show();
                }else {
                    window.top.xhTop.closeModal();
                    window.top.xhTop.getActiveFrame().resInList.dataGridReload();
                }
                if(ableFlag){
                    getBoxNum($('#sBoxCode').textbox('getValue'));
                    $('#sMeasure').combobox('disable');
                    $('#iTypeId').combobox('disable');
                    $('#iProdId').combobox('disable');
                    $('#sBoxCode').textbox('disable');
                }
            },
            error:function (data) {
                $.messager.alert('错误',data['responseText'],'error');
            }
        });
    });

    /**
     * 明细获取到箱内的boxCode
     * 
     * created by dale 2017126 
     */
    function getBoxNum(boxCode) {
        $.ajax({
            url:baseUrl+'ims/res/getBoxNum',
            data:{boxCode:boxCode},
            type:'post',
            dataType:'json',
            success:function (data) {
                if(data['total'] == data['current']){
                    $('#back').click();
                    $('.modal-title',parent.document).text('入库');
                    $.messager.alert('成功','该箱内所有平板均已导入','info');
                }else {
                    $('.modal-title',parent.document).text('明细-剩余需导入平板'+(data['total']-data['current'])+'个');
                }
            }
        })
    }

    /**
     * 获取量词
     * @param measure 量词字符串
     * @param single  true or false 单个
     */
    function getMeasure(measure,single) {
        var id;
        if(single==1){
            $.each(measure.split('/'),function (key,value) {
                if(meas['id']=='只'||meas['id']=='支'||meas['id']=='个'||meas['id']=='本'||meas['id']=='包'||meas['id']=='台'||meas['id']=='张'||meas['id']=='瓶'){
                    id=value;
                }
            });
            return id;
        }else{
            $.each(measure.split('/'),function (key,value) {
                if(meas['id']=='箱'||meas['id']=='袋'||meas['id']=='条'||meas['id']=='盒'){
                    id=value;
                }
            });

            return id;
        }
    }


});